<!--
 * @Author: Vi卡农
 * @Date: 2020-05-11 09:42:44
 * @LastEditTime: 2020-05-15 18:02:10
 * @Description: -录入意向-买-
 * @FilePath: \cshf_admin\src\views\modules\customer\page\intentionsellinfo-add-or-update.vue
 -->
<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="CloseDialog_"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      label-width="100px"
      class="fromStyle"
      :rules="rules"
      inline
      v-loading="loading"
      @keyup.enter.native="dataFormSubmitHandle()"
    >
      <!-- <el-form-item label="交易类型" prop="transactionType">
                        <el-input v-model="dataForm.transactionType" placeholder="交易类型"></el-input>
                    </el-form-item>
                    <el-form-item label="客源类型" prop="customerType">
                        <el-input v-model="dataForm.customerType" placeholder="客源类型"></el-input>
      </el-form-item>-->

      <el-form-item label="房源" prop="houseId">
        <el-input v-if="dataForm.id==null" v-model="dataForm.houseId" placeholder="房源" @focus="houseFocus"></el-input>
        <el-input  v-else v-model="dataForm.houseId" placeholder="房源" @focus="houseFocus" disabled></el-input>
      </el-form-item>

      <!-- <el-form-item label="房源" prop="houseId">
        <el-select
            v-model="dataForm.houseId"
            filterable
            remote
            clearable
            placeholder="请输入关键词"
            :remote-method="remoteMethod"
            :loading="loading"
            class="w100"
        >
            <el-option
                v-for="item in options"
                :key="item.id"
                :label="'FY'+item.id.substring(item.id.length-6)"
                :value="item.id"
                @click.native="houseIdClick(item)"
            ></el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item label="房号" prop="num">
        <el-input v-model="dataForm.num" placeholder="房号" disabled></el-input>
      </el-form-item>
      <el-form-item label="物业类型" prop="type">
        <ren-select
          v-model="dataForm.type"
          dict-type="dictType"
          placeholder="物业类型"
          class="FormSelect"
          style="width:100%"
          :disabled="true"
        ></ren-select>
      </el-form-item>
      <el-form-item label="交易类型" prop="transactionType">
        <ren-select
          v-model="dataForm.transactionType"
          dict-type="houseTransactionType"
          class="FormSelect"
          style="width:100%"
          disabled
        ></ren-select>
      </el-form-item>
      <el-form-item label="房源面积" prop="area">
        <el-input v-model="dataForm.area" placeholder="房源面积" disabled></el-input>
      </el-form-item>
      <!-- <el-form-item label="客源类型" prop="customerType">
        <el-input v-model="dataForm.customerType" placeholder="客源类型"></el-input>
      </el-form-item>-->
      <el-form-item label="客源编号" prop="customerId">
        <el-input v-model="dataForm.customerId" placeholder="客源id" disabled></el-input>
      </el-form-item>
      <!-- <el-form-item label="房间id" prop="fjId">
        <el-input v-model="dataForm.fjId" placeholder="房间id" disabled></el-input>
      </el-form-item>-->
      <el-form-item label="业主姓名" prop="ownerName">
        <el-input v-model="dataForm.ownerName" placeholder="业主姓名" disabled></el-input>
      </el-form-item>
      <el-form-item label="客户姓名" prop="customerName">
        <el-input v-model="dataForm.customerName" placeholder="客户姓名" disabled></el-input>
      </el-form-item>
      <el-form-item label="业主电话1" prop="ownerPhone1">
        <el-input v-model="dataForm.ownerPhone1" placeholder="业主电话" disabled></el-input>
      </el-form-item>

      <el-form-item label="客户电话1" prop="customerPhone1">
        <el-input v-model="dataForm.customerPhone1" placeholder="客户电话1" disabled></el-input>
      </el-form-item>
      <el-form-item label="业主电话2" prop="ownerPhone2">
        <el-input v-model="dataForm.ownerPhone2" placeholder="业主电话2" disabled></el-input>
      </el-form-item>

      <el-form-item label="客户电话2" prop="customerPhone2">
        <el-input v-model="dataForm.customerPhone2" placeholder="客户电话2" disabled></el-input>
      </el-form-item>
      <el-form-item label="业主电话3" prop="ownerPhone3">
        <el-input v-model="dataForm.ownerPhone3" placeholder="业主电话2" disabled></el-input>
      </el-form-item>

      <el-form-item label="客户电话3" prop="customerPhone3">
        <el-input v-model="dataForm.customerPhone3" placeholder="客户电话3" disabled></el-input>
      </el-form-item>
      <el-form-item label="房源经纪人" prop="houseAgentName">
        <el-input v-model="dataForm.houseAgentName" placeholder="房源经纪人" disabled></el-input>
      </el-form-item>
      <el-form-item label="客源经纪人" prop="customerAgentName">
        <el-input v-model="dataForm.customerAgentName" placeholder="客源经纪人" disabled></el-input>
      </el-form-item>

      <el-form-item label="单价" prop="storey">
        <el-row :gutter="20">
          <el-col :span="16">
            <div class="grid-content bg-purple">
              <el-input v-model="dataForm.price" placeholder="价格"></el-input>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-select v-model="dataForm.unit" style="width:100%" placeholder="单位">
                <el-option label="元" value="1"></el-option>
                <el-option label="万元" value="0"></el-option>
              </el-select>
            </div>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="总价" prop="prices">
        <el-input v-model="dataForm.prices" placeholder="总价"></el-input>
      </el-form-item>
      <el-form-item label="意向单进度" prop="state">
        <ren-select
          v-model="dataForm.state"
          dict-type="intentionState"
          placeholder="进度"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item v-if="dataForm.id==null" label="意向单状态" prop="status">
        <ren-select
          v-model="dataForm.status"
          dict-type="intentionStatus"
          placeholder="状态"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="佣金" prop="commission">
        <el-input v-model="dataForm.commission" placeholder="佣金"></el-input>
      </el-form-item>
      <el-form-item label="房东佣金" prop="ownerCommission">
        <el-row :gutter="20">
          <el-col :span="16">
            <div class="grid-content bg-purple">
              <el-input v-model="dataForm.ownerCommission" placeholder="房东佣金"></el-input>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-select v-model="dataForm.invoice" style="width:100%" placeholder="开票">
                <el-option label="开票" value="1"></el-option>
                <el-option label="不开票" value="2"></el-option>
                <el-option label="开收据" value="3"></el-option>
                <el-option label="不开收据" value="4"></el-option>
              </el-select>
            </div>
          </el-col>
        </el-row>
      </el-form-item>
      <!-- <el-form-item label="房东佣金" prop="ownerCommission">
        <el-input v-model="dataForm.ownerCommission" placeholder="房东佣金"></el-input>
      </el-form-item>-->
      <el-form-item label="客户佣金" prop="customerCommission">
        <el-row :gutter="20">
          <el-col :span="16">
            <div class="grid-content bg-purple">
              <el-input v-model="dataForm.customerCommission" placeholder="客户佣金"></el-input>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-select v-model="dataForm.cinvoice" style="width:100%" placeholder="收据">
                <el-option label="开票" value="1"></el-option>
                <el-option label="不开票" value="2"></el-option>
                <el-option label="开收据" value="3"></el-option>
                <el-option label="不开收据" value="4"></el-option>
              </el-select>
            </div>
          </el-col>
        </el-row>
      </el-form-item>
      <!-- <el-form-item label="客户佣金" prop="customerCommission">
        <el-input v-model="dataForm.customerCommission" placeholder="客户佣金"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="是否开票" prop="invoice">
        <el-input v-model="dataForm.invoice" placeholder="是否开票"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="开票佣金" prop="invoiceCommission">
        <el-input v-model="dataForm.invoiceCommission" placeholder="开票佣金"></el-input>
      </el-form-item>-->
      <el-form-item label="外佣" prop="externalCommission">
        <el-input v-model="dataForm.externalCommission" placeholder="外佣"></el-input>
      </el-form-item>
      <!-- <el-form-item label="平台佣金" prop="platformCommission">
        <el-input v-model="dataForm.platformCommission" placeholder="平台佣金"></el-input>
      </el-form-item>-->
      <el-form-item label="业绩佣金" prop="achievementCommission">
        <el-input v-model="dataForm.achievementCommission" placeholder="业绩佣金"></el-input>
      </el-form-item>
      <el-form-item label="税收" prop="taxation">
        <ren-select
          v-model="dataForm.taxation"
          placeholder="请选择"
          dict-type="houseTaxation"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>

      <el-form-item label="装修情况" prop="renovation">
        <ren-select
          v-model="dataForm.renovation"
          dict-type="dictRenovation"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="家具情况" prop="furniture">
        <el-select v-model="dataForm.furniture" class="FormSelect" style="width:100%">
          <el-option label="没有" value="0"></el-option>
          <el-option label="有" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="预计签约时间" prop="signingData">
        <el-date-picker
          v-model="dataForm.signingData"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width:100%"
        ></el-date-picker>
      </el-form-item>
      <div
        v-if="dataForm.customerType == '1'||dataForm.customerType == '3'||dataForm.customerType == '5'"
      >
        <el-form-item label="租赁月数" prop="tenanTime">
          <el-input v-model="dataForm.tenanTime" placeholder="租赁月数"></el-input>
        </el-form-item>
        <el-form-item label="免租天数" prop="tenanFreeTime">
          <el-input v-model="dataForm.tenanFreeTime" placeholder="免租天数"></el-input>
        </el-form-item>
        <el-form-item label="递增" prop="increase">
          <el-input v-model="dataForm.increase" placeholder="递增"></el-input>
        </el-form-item>
        <el-form-item label="付款方式" prop="tenanPay">
          <ren-select
            v-model="dataForm.tenanPay"
            dict-type="dictTenanPay"
            class="FormSelect"
            style="width:100%"
          ></ren-select>
        </el-form-item>
      </div>
      <div
        v-if="dataForm.customerType == '2'||dataForm.customerType == '4'||dataForm.customerType == '6'"
      >
        <el-form-item label="付款方式" prop="payType">
          <ren-select
            v-model="dataForm.payType"
            dict-type="dictOwnerBuy"
            class="FormSelect"
            style="width:100%"
          ></ren-select>
        </el-form-item>

        <el-form-item label="首付" prop="payments">
          <el-input v-model="dataForm.payments" placeholder="首付"></el-input>
        </el-form-item>
        <el-form-item label="房东抵押" prop="loanPrice">
          <el-input v-model="dataForm.loanPrice" placeholder="房东抵押"></el-input>
        </el-form-item>
        <el-form-item label="抵押信息" prop="mortgage">
          <el-input v-model="dataForm.mortgage" placeholder="抵押信息"></el-input>
        </el-form-item>
        <el-form-item label="发票" prop="buyBill">
          <el-select v-model="dataForm.buyBill" class="FormSelect" style="width:100%">
            <el-option label="没有" :value="0"></el-option>
            <el-option label="有" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="能否清场" prop="clearance">
          <el-select v-model="dataForm.clearance" class="FormSelect" style="width:100%">
            <el-option label="不能" :value="0"></el-option>
            <el-option label="能" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房源用途" prop="purpose">
          <ren-select
            v-model="dataForm.purpose"
            dict-type="housePurpose"
            class="FormSelect"
            style="width:100%"
          ></ren-select>
        </el-form-item>
      </div>

      <!-- <el-form-item label="删除状态" prop="deleteFlag">
                        <el-input v-model="dataForm.deleteFlag" placeholder="删除状态"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="城市租户编码" prop="tenantCode">
                        <el-input v-model="dataForm.tenantCode" placeholder="城市租户编码"></el-input>
      </el-form-item>-->
      <el-form-item label="配置" prop="conf" class="FormitemW100">
        <el-checkbox-group v-model="confList" @change="confListChange">
          <el-checkbox
            :label="item.dictValue"
            v-for="(item,index) in $getDictDataList('dictConf')"
            :key="index"
          >{{item.dictLabel}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark" class="FormitemW100">
            <el-input
              v-model="dataForm.remark"
              placeholder="备注"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
    <Housing @setHousing="setHousingFun" ref="HousingRef"></Housing>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
import Housing from "../../Housing/Housing";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        type: "",
        transactionType: "",
        customerType: this.$route.query.customerType,
        customerId: this.$route.query.ID,
        houseId: "",
        fjId: "",
        state: "",
        status: "",
        area: "",
        price: "",
        prices: "",
        unit: "",
        commission: "",
        ownerCommission: "",
        customerCommission: "",
        invoice: "",
        invoiceCommission: "",
        externalCommission: "",
        platformCommission: "",
        achievementCommission: "",
        houseAgentId: "",
        houseAgentName: "",
        customerAgentId: "",
        customerAgentName: "",
        customerName: "",
        customerPhone1: "",
        customerPhone2: "",
        customerPhone3: "",
        ownerName: "",
        ownerPhone1: "",
        ownerPhone2: "",
        ownerPhone3: "",
        taxation: "",
        renovation: "",
        furniture: "",
        conf: "",
        tenanTime: "",
        tenanFreeTime: "",
        increase: "",
        tenanPay: "",
        payType: "",
        payments: "",
        loanPrice: "",
        mortgage: "",
        buyBill: "",
        clearance: "",
        purpose: "",
        deptId: "",
        signingData: "",
        followDate: "",
        tenantCode: "",
        deleteFlag: "",
        creator: "",
        createDate: "",
        updater: "",
        updateDate: "",
        num: "",
        dictId: ""
      },
      options: [],
      loading: false,
      ifHousing: false,
      confList: [],
      rules: {
        commission: [
          {
            pattern: /^\d+(\.\d+)?$/,
            required: true,
            message: "佣金只能为大于0的数切不为空",
            trigger: "blur"
          }
        ],
        achievementCommission: [
          {
            pattern: /^\d+(\.\d+)?$/,
            required: true,
            message: "业绩佣金只能为大于0的数切不为空",
            trigger: "blur"
          }
        ],
        state: [
          { required: true, message: "请选择意向单进度", trigger: "change" }
        ],
        status: [
          { required: true, message: "请选择意向单状态", trigger: "change" }
        ]
      }
    };
  },
  components: {
    Housing
  },
  props: ["CustomerObj"],
  created() {
    if (this.CustomerObj) {
      this.dataForm.customerType = this.CustomerObj.customerType;
      this.dataForm.customerId = this.CustomerObj.id;
      this.dataForm.customerName = this.CustomerObj.customerName;
      this.dataForm.customerPhone1 = this.CustomerObj.customerPhone1;
      this.dataForm.customerPhone2 = this.CustomerObj.customerPhone2;
      this.dataForm.customerAgentId = this.CustomerObj.maintain;
      this.dataForm.customerAgentName = this.CustomerObj.maintainName;
    }
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/intention/intentioninfo/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/intention/intentioninfo/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
    confListChange(list) {
      this.dataForm.conf = list.join();
    },
    // 选择列表房源
    setHousingFun(obj) {
      // this.dataForm = {
      //   ...this.dataForm,
      //   ...obj
      // };
      // this.dataForm.houseId = obj.houseId;
      this.dataForm.type = obj.type;
      this.dataForm.houseId = obj.houseId;
      this.dataForm.num = obj.num;
      this.dataForm.dictId = obj.dictId;
      this.dataForm.houseType = obj.houseType;
      this.dataForm.transactionType = obj.transactionType;
      this.dataForm.fjId = obj.fjId;
      this.dataForm.ownerName = obj.ownerName;
      this.dataForm.ownerPhone1 = obj.ownerPhone1;
      this.dataForm.ownerPhone2 = obj.ownerPhone2;
      this.dataForm.area = obj.area;
      this.dataForm.price = obj.price;
      this.dataForm.prices = obj.prices;
      this.dataForm.houseAgentId = obj.maintain;
      this.dataForm.houseAgentName = obj.maintainName;
    },
    houseFocus() {
      this.$refs.HousingRef.info();
    },
    CloseDialog_() {
      this.$emit("CloseDialog");
    }
  }
};
</script>
